import { Evaluate } from 'evaluate-model.js';
var evaluate = new Evaluate();
var id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    appraise:'',
    loadingHidden: false,
    searchLoadingComplete: false,
    searchLoading: false,
    goodData:[],
    normalData:[],
    badData:[],
    allData:[],

  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    id = options.id;
    this._loadData(id)
  },
  statusTap: function (event) {
    var that = this;
    this.data.page = 1;
    var appraise = evaluate.getDataSet(event,'appraise');
    this.setData({
      appraise: appraise
    });
    if (appraise == 3) {
      if (this.data.goodData.length>0){
        that.setData({
          evalList: this.data.goodData,
          pageCount:this.data.goodCount,
          searchLoadingComplete: false,
          searchLoading: false,
        })
      }
      else{
        this._loadData(id);
      }
    }
    if (appraise == 2) {
      if (this.data.normalData.length > 0) {
        that.setData({
          evalList: this.data.normalData,
          pageCount: this.data.normalCount,
          searchLoadingComplete: false,
          searchLoading: false,
        })
      }
      else {
        this._loadData(id);
      }
    }
    if (appraise == 1) {
      if (this.data.badData.length>0) {
        that.setData({
          evalList: this.data.badData,
          pageCount: this.data.badCount,
          searchLoadingComplete: false,
          searchLoading: false,
        })
      }
      else {
        this._loadData(id);
      }
    }
    if (appraise == '') {
      if (this.data.allData.length > 0) {
        that.setData({
          evalList: this.data.allData,
          pageCount: this.data.allCount,
          searchLoadingComplete: false,
          searchLoading: false,
        })
      }
      else {
        this._loadData(id);
      }
    }
    if (this.data.pageCount == 1){
      that.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }

  },

  _loadData: function (id) {
    var that = this;
    var appraise = this.data.appraise;
    var page = this.data.page;
    var param = { 'goods_id': id, 'appraise': appraise }
    evaluate.getEvalLists(param, page,(res) => {
      var data = res.items;
      if (appraise==3){
        this.data.goodCount = res._meta.pageCount;
        this.data.goodData = res.items;
        that.setData({
          evalList: this.data.goodData,
          searchLoadingComplete: false,
        })
      }
      if (appraise == 2) {
        this.data.normalCount = res._meta.pageCount;
        this.data.normalData = res.items;
        that.setData({
          evalList: this.data.normalData,
          searchLoadingComplete: false,
        })
      }
      if (appraise == 1) {
        this.data.badCount = res._meta.pageCount;
        this.data.badData = res.items;
        that.setData({
          evalList: this.data.badData,
          searchLoadingComplete: false,
        })
      }
      if (appraise == '') {
        this.data.allCount = res._meta.pageCount;
        this.data.allData = res.items;
        that.setData({
          evalList: this.data.allData,
          searchLoadingComplete: false,
        })
      }
      that.setData({
        loadingHidden: true,
        pageCount:res._meta.pageCount
      })
      if (this.data.pageCount == 1) {
        that.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
      this.setData({
        'comment_count': res.comment_count
      })
    })
  },


  //触底滚动加载
  onReachBottom: function () {
    var page = parseInt(this.data.page)+1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })
      var param = { 'goods_id': id, 'appraise': this.data.appraise}
      evaluate.getEvalLists(param, page, (res) => {
        this.scrollLoadData(res, page);
      })
    }
  },
  //拼接数据
  scrollLoadData: function (res, page) {
    var data = res.items;
    var evalList = this.data.evalList;
    for (var i = 0; i < data.length; i++){
      data[i].created_at = evaluate.toDate(data[i].created_at)
    }
    evalList = evalList.concat(data);
    this.setData({
      evalList: evalList,
      page: page,
      pageCount:res._meta.pageCount,
    })
    if (page== this.data.pageCount) {
      this.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }
  },
})